package com.example.rabbit.demo.exchange.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @program: rabbit-demo
 * @description:
 * @author: zhaoliang
 * @create: 2019-12-30 13:38
 */
public class ProducerTopicExchange {

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.200.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2.通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();
        //3.通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4.通过channel发送数据
        String exchangeName="test_topic_exchange";
        String routingKey1="user.save";
        String routingKey2="user.update";
        String routingKey3="user.delete.abc";
        String routingKey4="order.save";

        String msg1="Producer topic_exchange生产的消息"+routingKey1;
        String msg2="Producer topic_exchange生产的消息"+routingKey2;
        String msg3="Producer topic_exchange生产的消息"+routingKey3;
        String msg4="Producer topic_exchange生产的消息"+routingKey4;
        channel.basicPublish(exchangeName,routingKey1,null,msg1.getBytes());
        channel.basicPublish(exchangeName,routingKey2,null,msg2.getBytes());
        channel.basicPublish(exchangeName,routingKey3,null,msg3.getBytes());
        channel.basicPublish(exchangeName,routingKey4,null,msg4.getBytes());

        //5.关闭相关连接
        channel.close();
        connection.close();

    }
}
